package com.ebelter.btcomlib.models.bluetooth.products.bracelet;

import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Calendar;


public class BraceletBytesMake {

    private static final String TAG = "BraceletBytesMake";
    private static final byte _0 = 0;

    //同步时间
    public SendMessage sysTime(){
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR)-2000;
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        int todayWeek = now.get(Calendar.DAY_OF_WEEK) - 2;
        if (todayWeek == -1) {
            todayWeek = 6;
        }
        byte[] cmd = new byte[20];
        cmd[0] = 0x03;
        cmd[1] = 0x01;
        cmd[2] = (byte) year;
        cmd[3] = (byte) month;
        cmd[4] = (byte) day;
        cmd[5] = (byte) hour;
        cmd[6] = (byte) minute;
        cmd[7] = (byte) second;
       // cmd[8] = (byte) todayWeek;
        for (int i = 8; i < 20; i++) {
            cmd[i] = 0x00;
        }
        SendMessage msg=new SendMessage();
        msg.desc="设置设备时间";
        msg.datas=cmd;
        LogUtils.i(TAG,"下发的时间="+BytesUtils.bytes2HexStr(cmd));

        return msg;
    }

    //设置用户信息
    public SendMessage setUserInfo(int height,int weight,int sex) {
        byte[] cmd = new byte[20];
        cmd[0] = 0x03;
        cmd[1] = 0x10;
        cmd[2] = (byte)height;
        cmd[3] = (byte)weight;
        cmd[4] = (byte)sex;
        for (int i = 5; i < 20; i++) {
            cmd[i] = _0;
        }
        SendMessage msg=new SendMessage();
        msg.desc="设置用户信息";
        msg.datas=cmd;
        LogUtils.i(TAG,"下发的用户信息bytes = "+ BytesUtils.bytes2HexStr(msg.datas));
        return msg;
    }

    /**
     * 设置抬腕亮屏
     * true: 开启  false:  关闭
     */
    public SendMessage setUPHandGesture(boolean isOpen) {
        byte[] cmd = new byte[20];
        cmd[0] = 0x03;
        cmd[1] = 0x28;
        cmd[2] = (byte) (isOpen ? 1 : 0);
        for (int i = 3; i < 20; i++) {
            cmd[i] = _0;
        }
        SendMessage msg=new SendMessage();
        msg.desc="设置抬腕亮屏 isOpen = "+isOpen;
        msg.datas=cmd;
        return msg;
    }

    //设置测量心率周期
    public SendMessage setHeartCycle(int minutes){
        byte[] cmd = new byte[20];
        cmd[0] =0x03;
        cmd[1] =0x26;
        cmd[2]=(byte)minutes;
        for (int i = 3; i < 20; i++) {
            cmd[i] = _0;
        }
        SendMessage msg=new SendMessage();
        msg.desc="设置心率周期 minutes = " + minutes;
        msg.datas=cmd;
        return msg;
    }

    //获取实时数据
    public SendMessage getLiveData(){
        SendMessage msg=new SendMessage();
        msg.desc="获取实时数据";
        msg.datas=sendCmd((byte) 0x02,(byte) 0x07);
        return msg;
    }

    public SendMessage getVersionData() {
        SendMessage msg=new SendMessage();
        msg.desc="获取固件版本号";
        msg.datas=sendCmd((byte) 0x02,(byte) 0x01);
        return msg;
    }

    //关闭消息通知
    public SendMessage getCloseNotification() {
        SendMessage msg=new SendMessage();
        msg.desc="关闭消息通知";
        msg.datas=sendCmd((byte) 0x03,(byte) 0x30);
        return msg;
    }

    //打开或者关闭实时心率
    public SendMessage getHeartRate(boolean isOpen) {
        byte[] cmd = new byte[20];
        cmd[0] = 0x02;
        cmd[1] = 0x09;
        cmd[2] = (byte) (isOpen?1:0);
        for (int i = 3; i < 20; i++) {
            cmd[i] = _0;
        }
        SendMessage msg=new SendMessage();
        msg.desc="打开或者关闭实时心率 isOpen = "+isOpen;
        msg.datas=cmd;
        return msg;
    }













    private byte[] sendCmd(byte cmdId, byte key) {
        byte[] cmd = new byte[20];
        cmd[0] = cmdId;
        cmd[1] = key;
        for (int i = 2; i < 20; i++) {
            cmd[i] = _0;
        }
        return cmd;
    }


    public SendMessage getOtaUpdateCmd() {
        SendMessage msg=new SendMessage();
        msg.desc="下发ota升级命令";
        msg.datas=sendCmd((byte) 0x01,(byte) 0x01);
        return msg;

    }
}
